package com.jerkin.zookeeper;

import org.apache.curator.framework.CuratorFramework;
import org.apache.curator.framework.recipes.cache.ChildData;
import org.apache.curator.framework.recipes.cache.NodeCache;
import org.apache.curator.framework.recipes.cache.NodeCacheListener;
import org.apache.curator.framework.recipes.cache.PathChildrenCache;
import org.apache.curator.framework.recipes.cache.PathChildrenCacheEvent;
import org.apache.curator.framework.recipes.cache.PathChildrenCacheListener;
import org.apache.zookeeper.CreateMode;

/**
 * @version V1.0
 * @ClassName: ZKTest
 * @Description:
 * @author: huhongzhi
 * @date: 2019/12/16 16:51
 */
public class ZKTest {

    public static void main(String[] args) throws Exception {
        CuratorFramework curatorFramework = ZKFactory.createZK();
//        curatorFramework.start();
        ZKNodeListener.listen().start();
        watchDataChange(curatorFramework,"/node10/node1");
        watchChild(curatorFramework,"/node10");
        curatorFramework.create()
                .creatingParentsIfNeeded()
                .withMode(CreateMode.PERSISTENT)
                .forPath("/node10/node1", "node1".getBytes());
        System.out.println("创建了持久节点");
        Thread.sleep(1000);
        curatorFramework.setData().forPath("/node10/node1","测试".getBytes());

    }

    private static void watchDataChange(CuratorFramework curatorFramework, String path) throws Exception {
        final NodeCache nodeCache = new NodeCache(curatorFramework, path, false);
        nodeCache.getListenable().addListener(new NodeCacheListener() {
            @Override
            public void nodeChanged() throws Exception {
                System.out.println("开始监听本级节点-----");
                System.out.println("监听的节点为" + nodeCache.getCurrentData().getPath() + "数据变为 : "+new String(nodeCache.getCurrentData().getData()));

            }
        });
        nodeCache.start();
    }

    //创建本节点的子节点监听事件
    public static void watchChild(CuratorFramework curator, String path) throws Exception{
        PathChildrenCache childrenCache = new PathChildrenCache(curator, path, true);
        PathChildrenCacheListener childrenCacheListener = new PathChildrenCacheListener() {

            @Override
            public void childEvent(CuratorFramework client,
                                   PathChildrenCacheEvent event) throws Exception {
                System.out.println("开始监听子节点:-----");
                ChildData data = event.getData();
                switch (event.getType()) {
                    case CHILD_ADDED:
                        System.out.println("CHILD_ADDED : "+ data.getPath() +"  数据:"+ data.getData());
                        break;
                    case CHILD_REMOVED:
                        System.out.println("CHILD_REMOVED : "+ data.getPath() +"  数据:"+ data.getData());
                        break;
                    case CHILD_UPDATED:
                        System.out.println("CHILD_UPDATED : "+ data.getPath() +"  数据:"+ new String(data.getData()));
                        break;
                    default:
                        break;
                }
            }

        };

        childrenCache.getListenable().addListener(childrenCacheListener);
        System.out.println("Register zk watcher successfully!");
        childrenCache.start(PathChildrenCache.StartMode.POST_INITIALIZED_EVENT);
    }
}
